import {LOGIN, COUNT_USER, ROUTES, LIST_USERS, DELETE_USER,CFREATE_USER,UPDATE_PASSWORD} from '@/services/api'
import {request, METHOD,removeAuthorization} from '@/utils/request'

/**
 * 登录服务
 * @param name 账户名
 * @param password 账户密码
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function login(name, password) {
  let data = new FormData();
  data.append('username',name);
  data.append('password',password);
  return request(LOGIN, METHOD.POST, data)
}

export async function countUser() {
  return request(COUNT_USER, METHOD.GET)
}
export async function listUsers(pageNum,pageSize) {
  let data = new FormData();
  data.append('pageNum',pageNum);
  data.append('pageSize',pageSize);
  return request(LIST_USERS, METHOD.POST,data)
}

export async function createUser(username,password) {
  let data = new FormData();
  data.append('username',username);
  data.append('password',password);
  return request(CFREATE_USER, METHOD.POST,data)
}

export async function getRoutesConfig() {
  return request(ROUTES, METHOD.GET)
}

/**
 * 退出登录
 */
export function logout() {
  localStorage.removeItem(process.env.VUE_APP_ROUTES_KEY)
  localStorage.removeItem(process.env.VUE_APP_PERMISSIONS_KEY)
  localStorage.removeItem(process.env.VUE_APP_ROLES_KEY)
  removeAuthorization()
}

export async function deleteUser(userId) {
  let data = new FormData();
  data.append('userId',userId);
  return request(DELETE_USER, METHOD.POST,data)
}

export async function updatePassword(userId,newPass) {
  let data = new FormData();
  data.append('userId',userId);
  data.append('newPass',newPass);
  return request(UPDATE_PASSWORD, METHOD.POST,data)
}

export default {
  login,
  logout,
  countUser,
  listUsers,
  getRoutesConfig,
  deleteUser,
  updatePassword,
  createUser
}
